package com.motionlog.activity;

import java.util.Stack;

import com.motionlog.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Password extends Activity implements OnClickListener {

	Stack<Integer> pass = new Stack<Integer>();

	View state1;
	View state2;
	View state3;
	View state4;

	boolean setPassword = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.password);

		String action = getIntent().getStringExtra("action");
		if (action != null) {
			// 设置密码
			setPassword = true;
		}

		Button num1 = (Button) findViewById(R.id.num1);
		Button num2 = (Button) findViewById(R.id.num2);
		Button num3 = (Button) findViewById(R.id.num3);
		Button num4 = (Button) findViewById(R.id.num4);
		Button num5 = (Button) findViewById(R.id.num5);
		Button num6 = (Button) findViewById(R.id.num6);
		Button num7 = (Button) findViewById(R.id.num7);
		Button num8 = (Button) findViewById(R.id.num8);
		Button num9 = (Button) findViewById(R.id.num9);
		Button num0 = (Button) findViewById(R.id.num0);
		Button backspace = (Button) findViewById(R.id.backspace);
		num1.setOnClickListener(this);
		num2.setOnClickListener(this);
		num3.setOnClickListener(this);
		num4.setOnClickListener(this);
		num5.setOnClickListener(this);
		num6.setOnClickListener(this);
		num7.setOnClickListener(this);
		num8.setOnClickListener(this);
		num9.setOnClickListener(this);
		num0.setOnClickListener(this);
		backspace.setOnClickListener(this);

		state1 = findViewById(R.id.state1);
		state2 = findViewById(R.id.state2);
		state3 = findViewById(R.id.state3);
		state4 = findViewById(R.id.state4);
	}

	private void checkPassword() {
		Integer[] passes = new Integer[4];
		pass.copyInto(passes);
		StringBuilder sb = new StringBuilder();
		for (Integer i : passes) {
			sb.append(i);
		}

		SharedPreferences s = getSharedPreferences("settings", MODE_PRIVATE);
		String password = s.getString("password-value", "1357");

		if (sb.toString().equals(password)) {
			startHome();
		} else {
			Toast.makeText(this, "密码输入错误，请重新输入", Toast.LENGTH_LONG).show();
		}
	}

	private void startHome() {
		SharedPreferences sets = getSharedPreferences("settings", MODE_PRIVATE);
		boolean hasTutor = sets.getBoolean("tutor", true);
		Class startActivity;
		if (hasTutor) {
			startActivity = Tutorial.class;
			sets.edit().putBoolean("tutor", false);//.apply(); ////gggggg
			sets.edit().commit();
		} else {
			startActivity = Home.class;
		}
		Intent i = new Intent(this, startActivity);
		startActivity(i);
		finish();
	}

	private void updateState() {
		state1.setBackgroundResource(R.drawable.radiobutton);
		state2.setBackgroundResource(R.drawable.radiobutton);
		state3.setBackgroundResource(R.drawable.radiobutton);
		state4.setBackgroundResource(R.drawable.radiobutton);
		switch (pass.size()) {
		case 4:
			state4.setBackgroundResource(R.drawable.radiobutton_choose);
		case 3:
			state3.setBackgroundResource(R.drawable.radiobutton_choose);
		case 2:
			state2.setBackgroundResource(R.drawable.radiobutton_choose);
		case 1:
			state1.setBackgroundResource(R.drawable.radiobutton_choose);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backspace:
			if (!pass.isEmpty()) {
				pass.pop();
				updateState();
			}
			break;
		case R.id.unkown:
			break;
		default:
			Button btn = (Button) v;
			pass.push(Integer.valueOf(btn.getText().toString()));
			if (pass.size() == 4) {
				if (setPassword) {
					Integer[] passes = new Integer[4];
					pass.copyInto(passes);
					StringBuilder sb = new StringBuilder();
					for (Integer i : passes) {
						sb.append(i);
					}
/*
					SharedPreferences s = getSharedPreferences("settings",
							MODE_PRIVATE);
					s.edit().putString("password-value", sb.toString())
							.putBoolean("password", true).commit();

					startHome();*/
					checkPassword();
				} else {
					startHome();
				}
				pass.clear();
			}
			updateState();
			break;
		}
	}
}
